package com.jeethink.erp.commodity.controller;

import java.util.ArrayList;
import java.util.List;

import com.jeethink.common.core.domain.entity.TbCommodityType;
import com.jeethink.common.core.domain.entity.TbShop;
import com.jeethink.erp.shop.service.ITbShopService;
import com.jeethink.erp.type.service.ITbCommodityTypeService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.jeethink.common.annotation.Log;
import com.jeethink.common.core.controller.BaseController;
import com.jeethink.common.core.domain.AjaxResult;
import com.jeethink.common.enums.BusinessType;
import com.jeethink.erp.commodity.domain.TbCommodity;
import com.jeethink.erp.commodity.service.ITbCommodityService;
import com.jeethink.common.utils.poi.ExcelUtil;
import com.jeethink.common.core.page.TableDataInfo;

/**
 * 商品基本信息Controller
 * 
 * @author ypr
 * @date 2021-05-26
 */
@RestController
@RequestMapping("/erp/commodity")
public class TbCommodityController extends BaseController
{
    @Autowired
    private ITbCommodityService tbCommodityService;

    @Autowired
    private ITbShopService tbShopService;

    @Autowired
    private ITbCommodityTypeService tbCommodityTypeService;

    /**
     * 查询商品基本信息列表
     */
    @PreAuthorize("@ss.hasPermi('erp:commodity:list')")
    @GetMapping("/list")
    public TableDataInfo list(TbCommodity tbCommodity)
    {
        startPage();
        List<TbCommodity> list = tbCommodityService.selectTbCommodityList(tbCommodity);
        return getDataTable(list);
    }

    /**
     * 导出商品基本信息列表
     */
    @PreAuthorize("@ss.hasPermi('erp:commodity:export')")
    @Log(title = "商品基本信息", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TbCommodity tbCommodity)
    {
        List<TbCommodity> list = tbCommodityService.selectTbCommodityList(tbCommodity);
        ExcelUtil<TbCommodity> util = new ExcelUtil<TbCommodity>(TbCommodity.class);
        return util.exportExcel(list, "commodity");
    }

    /**
     * 获取商品基本信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('erp:commodity:query')")
    @GetMapping(value = "/{commodityId}")
    public AjaxResult getInfo(@PathVariable("commodityId") Long commodityId)
    {
        return AjaxResult.success(tbCommodityService.selectTbCommodityById(commodityId));
    }

    /**
     * 新增商品基本信息
     */
    @PreAuthorize("@ss.hasPermi('erp:commodity:add')")
    @Log(title = "商品基本信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TbCommodity tbCommodity)
    {
        return toAjax(tbCommodityService.insertTbCommodity(tbCommodity));
    }

    /**
     * 修改商品基本信息
     */
    @PreAuthorize("@ss.hasPermi('erp:commodity:edit')")
    @Log(title = "商品基本信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TbCommodity tbCommodity)
    {
        return toAjax(tbCommodityService.updateTbCommodity(tbCommodity));
    }

    /**
     * 删除商品基本信息
     */
    @PreAuthorize("@ss.hasPermi('erp:commodity:remove')")
    @Log(title = "商品基本信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{commodityIds}")
    public AjaxResult remove(@PathVariable Long[] commodityIds)
    {
        return toAjax(tbCommodityService.deleteTbCommodityByIds(commodityIds));
    }

    @GetMapping("/commodityList")
    public AjaxResult commodityList(){
        List<TbCommodity> commodityList = tbCommodityService.selectTbCommodityList(new TbCommodity());
        return AjaxResult.success(commodityList);
    }

    @GetMapping("/mineStock")
    public TableDataInfo mineStock(Long shopId){
        TbShop getShop = tbShopService.selectTbShopById(shopId);
        List<TbCommodity> getMineStockList = null;

        //判断是否是一级店铺 如果是一级店铺 则查询所有库存
        if(getShop.getParentId() == 0){
            getMineStockList = tbCommodityService.getMineStockList(new TbCommodity());
        }else {
            TbCommodity tbCommodity = new TbCommodity();
            tbCommodity.setShopId(shopId);
            getMineStockList = tbCommodityService.mineStock(tbCommodity);
        }



        return getDataTable(getMineStockList);
    }

    @GetMapping("/getCommodityTypeList")
    public AjaxResult getCommodityTypeList(Long commodityTypeId){
        List<TbCommodityType> findTypeIds = tbCommodityTypeService.selectTypeIds(commodityTypeId);
        List<Long> list = new ArrayList<>();
        List<TbCommodity> commodityList = new ArrayList<>();
        if(findTypeIds != null && findTypeIds.size()>0){
            for (int i=0;i<findTypeIds.size();i++){
                list.add(findTypeIds.get(i).getCommodityTypeId());
            }
            Long[] array = list.toArray(new Long[list.size()]);//能正确运行
            commodityList = tbCommodityService.selectCommodityTypeList(array);
        }

        return AjaxResult.success(commodityList);
    }

    @GetMapping("/viewInventory")
    public TableDataInfo viewInventory(Long shopId,String commodityName){

        TbCommodity tbCommodity = new TbCommodity();
        tbCommodity.setShopId(shopId);
        tbCommodity.setCommodityName(commodityName);
        List<TbCommodity> getMineStockList = tbCommodityService.mineStock(tbCommodity);

        return getDataTable(getMineStockList);
    }


}
